package com.mrs.gauss.interfaces.rest;

import com.mrs.gauss.common.ErrorCode;
import com.mrs.gauss.exception.BusinessException;
import com.mrs.gauss.interfaces.utils.HttpClientUtil;
import com.mrs.gauss.interfaces.utils.LoginClient;
import lombok.extern.log4j.Log4j;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

@Log4j
public class YarnRunning {
    public  List<Map> getYarnRunningTaskList() throws Exception {
        String userFilePath = "./conf/UserInfo.properties";
        InputStream userInfo = null;

        File file = new File(userFilePath);
        if (!file.exists()) {
            System.out.println("The user info file doesn't exist.");
        }// 认证用户
        userInfo = new BufferedInputStream(new FileInputStream(file));
        ResourceBundle resourceBundle = new PropertyResourceBundle(userInfo);
        String principal = resourceBundle.getString("userName");
        String ipAddr = resourceBundle.getString("ResourcesManger_Master");
        String keytab = "./conf/user.keytab";        // user.keytab路径
        String krb5 = "./conf/krb5.conf";            // krb5.conf路径

        System.setProperty("java.security.krb5.conf", krb5);
        String url = ipAddr + "/ws/v1/cluster/";
        try {
            LoginClient.getInstance().setConfigure(url, principal, keytab, "");
            LoginClient.getInstance().login();
            List<Map> runningTaskList  = HttpClientUtil.doGet(url, "utf-8");

            return runningTaskList;
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "getYarnRunningTaskList failed");
        }

    }
}
